package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import model.Card;
import model.CardType;
import model.Player;
import model.Territory;
import model.Player.PlayerTurn;

/**
 * PlayerPanel serves as the space for Player representation
 * @author Cam Adamson
 *
 */
public class PlayerPanel extends JPanel {
	
	private Player player;
	private UUID clientID;
	private JLabel FieldTroops;
	private JLabel BonusTroops;
	private JPanel troopInfo;
	private JLabel name;
	private JPanel playerButtons;
	private JButton playerCardsB;
	private List<Card> cards;


	/**
	 * Creates a player panel, for display in GUI, incluldes image, card amount, cards button
	 * num troop info, and name
	 * @param p
	 * @param thisP
	 */

	public PlayerPanel(Player p, UUID thisP) {
		this.player = p;
		clientID = thisP;

		this.setLayout(new BorderLayout());

		JLabel playerImage = new JLabel();
		playerImage.setIcon(player.getImage());

		FieldTroops = new JLabel("FieldTroops: " + player.getNumFieldArmies());
		BonusTroops = new JLabel("Bonus: " + player.getBonus());
		troopInfo = new JPanel();
		troopInfo.add(FieldTroops, BorderLayout.NORTH);
		troopInfo.add(BonusTroops, BorderLayout.SOUTH);

		this.add(playerImage, BorderLayout.WEST);
		this.add(troopInfo, BorderLayout.SOUTH);
		name = new JLabel();
		if (player.getTurnPhase() != PlayerTurn.FALSE) {
			name.setText(player.getName());
		} else {
			name.setText(player.getName());
		}

		this.add(name, BorderLayout.NORTH);

		playerButtons = new JPanel();
		playerButtons.setMaximumSize(new Dimension(80, 100));
		playerButtons.setMinimumSize(new Dimension(80, 100));
		playerButtons.setPreferredSize(new Dimension(80, 100));

		setPlayerButtons();

		this.add(playerButtons, BorderLayout.CENTER);

		LineBorder border = new LineBorder(player.getColor(), 5);
		this.setBorder(border);

	}

	private void setPlayerButtons() {
		if (player.getUUID().compareTo(clientID) == 0) {
			playerButtons.setLayout(new BorderLayout());

			getCardButton(); // make playerCardsB

			playerButtons.add(playerCardsB, BorderLayout.SOUTH);

			JButton dice = new JButton();
			ImageIcon diceImage = new ImageIcon("images/dice.png");
			dice.setIcon(diceImage);
			dice.addActionListener(new DiceActionListener(player));
			playerButtons.add(dice, BorderLayout.NORTH);

		} else {
			JLabel playerCards = new JLabel();
			playerCards.setIcon(new ImageIcon("images/Risk Card-small.png"));
			playerCards.setText(" " + player.getNumCards());
			playerButtons.add(playerCards, BorderLayout.SOUTH);
		}

	}

	private JButton getCardButton() {
		playerCardsB = new JButton();
		playerCardsB.addActionListener(new CardsActionListener(player));
		playerCardsB.setIcon(new ImageIcon("images/Risk Card-small.png"));
		playerCardsB.setText("" + player.getNumCards());
		return playerCardsB;
	}

	private class CardsActionListener implements ActionListener {
		private Player player;

		public CardsActionListener(Player p) {
			player = p;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			cards = player.getCards();

			JFrame cardFrame = new JFrame();
			GridLayout picLayout = new GridLayout(1, 0);
			picLayout.setHgap(5);

			JPanel gridCards = new JPanel();
			gridCards.setLayout(picLayout);

			int index = 0;
			Iterator<Card> it = cards.iterator();
			if (cards.size() == 0) {
				JOptionPane
						.showMessageDialog(null,
								"Sorry, but you gotta nuke some fools to get some cards");
			}
			while (it.hasNext()) {
				Card card = it.next();

				ImageIcon image = new ImageIcon("images/cards/"
						+ card.getType().toString() + ".png");
				JLabel imageLabel = new JLabel();
				imageLabel.setIcon(image);
				JLabel cardLabel;
				if (card.getType() != CardType.WILD) {
					cardLabel = new JLabel(card.getTerritory()
							.getTerritoryName());
					// cardLabel = new JLabel("TEMP LABEL");
				} else {
					cardLabel = new JLabel("WILD");
				}

				JPanel cardPanel = new JPanel();
				cardPanel.setLayout(new BorderLayout());
				cardPanel.add(imageLabel, BorderLayout.CENTER);
				cardPanel.add(cardLabel, BorderLayout.NORTH);
				gridCards.add(cardPanel);

				index++;
			}
			cardFrame.add(gridCards);
			gridCards.setVisible(true);
			cardFrame.pack();
			cardFrame.setVisible(true);
		}
	}

	private class DiceActionListener implements ActionListener {
		private Player player;

		public DiceActionListener(Player p) {
			player = p;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			new DiceFrame(player);
		}
	}
}
